<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
  <head>
    <th:block th:include="include :: header('学习材料详情')" />
    <style>
      .dangnei-content {
        padding: 20px;
        background: #fff;
        border-radius: 4px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
      }
      .dangnei-title {
        font-size: 24px;
        font-weight: bold;
        text-align: center;
        margin-bottom: 20px;
        color: #c52222;
        border-bottom: 2px solid #c52222;
        padding-bottom: 15px;
      }
      .dangnei-meta {
        text-align: center;
        margin-bottom: 30px;
        color: #666;
        font-size: 14px;
      }
      .dangnei-text {
        line-height: 1.8;
        font-size: 16px;
        color: #333;
      }
      .dangnei-pdf,
      .dangnei-image {
        text-align: center;
        margin: 20px 0;
      }
      .dangnei-pdf iframe {
        width: 100%;
        height: 600px;
        border: 1px solid #ddd;
      }
      .dangnei-image img {
        max-width: 100%;
        height: auto;
        border: 1px solid #ddd;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
      }
      .no-content {
        text-align: center;
        padding: 50px;
        color: #999;
        font-size: 18px;
      }
      .pdf-preview-fallback {
        padding: 20px;
        background: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 4px;
        margin: 20px 0;
      }
      .pdf-preview-fallback a {
        color: #007bff;
        text-decoration: none;
      }
      .pdf-preview-fallback a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
      <div class="row">
        <div class="col-sm-12">
          <div class="dangnei-content">
            <div class="dangnei-title" th:text="${commonCms.title}">标题</div>
            <div class="dangnei-meta">
              <span
                >发布时间：<span
                  th:text="${#dates.format(commonCms.publishTime, 'yyyy-MM-dd HH:mm:ss')}"
                  >2023-01-01 12:00:00</span
                ></span
              >
            </div>

            <!-- 文字内容 -->
            <div
              class="dangnei-text"
              th:if="${commonCms.contentType == 'text' and !#strings.isEmpty(commonCms.contentUrl)}"
              th:utext="${commonCms.contentUrl}"
            >
              制度内容
            </div>

            <!-- PDF内容 -->
            <div
              class="dangnei-pdf"
              th:if="${commonCms.contentType == 'pdf' and !#strings.isEmpty(commonCms.contentUrl)}"
            >
              <iframe
                th:src="@{${commonCms.contentUrl}}"
                frameborder="0"
              ></iframe>
              <!-- 添加备用方案，如果iframe无法加载则提供下载链接 -->
              <div class="pdf-preview-fallback">
                <p>
                  如果PDF无法正常显示，请<a
                    th:href="@{${commonCms.contentUrl}}"
                    target="_blank"
                    >点击此处下载PDF文件</a
                  >进行查看。
                </p>
              </div>
            </div>

            <!-- 图片内容 -->
            <div
              class="dangnei-image"
              th:if="${commonCms.contentType == 'image' and !#strings.isEmpty(commonCms.contentUrl)}"
            >
              <img th:src="@{${commonCms.contentUrl}}" alt="制度内容" />
            </div>

            <!-- 无内容提示 -->
            <div
              class="no-contentUrl"
              th:if="${#strings.isEmpty(commonCms.contentUrl) and #strings.isEmpty(commonCms.contentUrl)}"
            >
              暂无内容
            </div>
          </div>
        </div>
      </div>
    </div>

    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
      // 获取材料ID
      var materialId = /*[[${commonCms.id}]]*/ 0;

      $(document).ready(function () {
        // 检查PDF是否加载成功
        $(".dangnei-pdf iframe")
          .on("load", function () {
            try {
              // 尝试访问iframe内容，如果失败说明加载有问题
              var iframeDoc =
                this.contentDocument || this.contentWindow.document;
              // 如果iframe内容为空或出错，显示备用方案
            } catch (e) {
              // 跨域问题，无法检查内容，但不影响使用
            }
          })
          .on("error", function () {
            // iframe加载出错时的处理
            console.log("PDF加载出错");
          });
      });
    </script>
  </body>
</html>
